package com.cpa.admin.service;

import com.cpa.admin.base.BaseService;
import com.cpa.admin.mapper.UserMapper;
import com.cpa.admin.mapper.UserOrderMapper;
import com.cpa.admin.model.UserDo;
import com.cpa.admin.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.DigestUtils;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * @author zhen.zhao01
 * @CreateDate 2018/10/31
 */
@Slf4j
@Service
public class UserService extends BaseService<UserDo, UserMapper> implements UserMapper {

    @Value("${password.salt}")
    private String passwordSalt;

    private final UserOrderMapper userOrderMapper;

    @Autowired
    public UserService(UserOrderMapper userOrderMapper) {
        this.userOrderMapper = userOrderMapper;
    }

    /**
     * 验证用户名密码
     *
     * @param username
     * @param password
     * @return
     */
    public boolean verify(String username, String password) {

        String oldPassword = mapper.getPasswordByUsername(username);

        if (StringUtils.isEmpty(oldPassword)) {
            return false;
        }

        if (!DigestUtils.md5DigestAsHex((passwordSalt + password).getBytes()).equals(oldPassword)) {
            return false;
        }

        return true;
    }


    @Override
    public UserDo findByUsername(String username) {
        return mapper.findByUsername(username);
    }

    @Override
    public String getPasswordByUsername(String username) {
        return mapper.getPasswordByUsername(username);
    }

    @Override
    public Boolean isAdmin(String username) {
        return mapper.isAdmin(username);
    }

    @Override
    public List<UserVo> findAllVoByView(String username) {
        return mapper.findAllVoByView(StringUtils.isEmpty(username)? null : "%" + username + "%");
    }

    @Override
    public String getId(String username) {
        return mapper.getId(username);
    }

    /**
     * 更新用户
     * @param userDo
     */
    public void updateUser(UserDo userDo) {
        UserDo oldDo = selectByPrimaryKey(userDo.getId());
        oldDo.setUsername(userDo.getUsername());
        if (!StringUtils.isEmpty(userDo.getPassword())) {
            oldDo.setPassword(DigestUtils.md5DigestAsHex((passwordSalt + userDo.getPassword()).getBytes()));
        }
        oldDo.setSettleAmountPushTime(userDo.getSettleAmountPushTime());

        oldDo.setUpdateTime(new Date());
        updateByPrimaryKeySelective(oldDo);
    }

    /**
     * 删除用户和订单表
     * @param userId
     */
    @Transactional(rollbackFor = Exception.class)
    public void deleteUserAndOrder(String userId) {

        UserDo userDo = selectByPrimaryKey(userId);
        userDo.setDeleteFlag(1);
        updateByPrimaryKeySelective(userDo);

//        userOrderMapper.deleteByUserId(userId);

    }
}
